package com.foo.CR;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: Tejas
 * Date: 12/28/10
 * Time: 5:08 PM
 */

/**
 * This class abstracts the shared preferences as a Map.
 * A shared preference file is created that stores a mapping <Wifi_SSID, ACTION>
 * This helps the application to persist the settings.
 */
public class MyWiFiMap
{
    private static final String PREF_NAME = "WIFIPrefs";
    private static MyWiFiMap _instance = null;
    private SharedPreferences _sharedPreferences;
    private Context _context;

    private MyWiFiMap(Context context)
    {
        _context = context;
        _sharedPreferences = context.getSharedPreferences(PREF_NAME, 0);
    }

    public static MyWiFiMap getMyWifiMap(Context context)
    {
        if(_instance == null)
            _instance = new MyWiFiMap(context);

        return _instance;
    }

    public void addOrUpdate(String wifiSSID, int action)
    {
        SharedPreferences.Editor editor = _sharedPreferences.edit();
        editor.putInt(wifiSSID, action);
        editor.commit();
    }

    public void remove(String wifiSSID)
    {
        SharedPreferences.Editor editor = _sharedPreferences.edit();
        editor.remove(wifiSSID);
        editor.commit();
    }

    public int getActionforSSID(String wifiSSID)
    {
        return _sharedPreferences.getInt(wifiSSID, Constants.RINGER_ACTION_NOT_FOUND);
    }

    public void clear()
    {
        SharedPreferences.Editor editor = _sharedPreferences.edit();
        editor.clear();
        // No commit req'd acc. to documentation
    }

    // Make this obsolete ?
    public Map getAllWiFis()
    {
       return _sharedPreferences.getAll();
    }

}
